package com.neutech.tank;

import com.neutech.Util.ImageUtils;
import com.neutech.base.*;
import com.neutech.constant.Constant;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 继承精灵
 */
public class Tank extends Sprite implements Moveable, Drawable {
    /**
     *           1     2     3     4
     * 五个级别 基础   快枪  双子单  大坦克
     */
    private int level = 1;
    /**
     * 子弹集合
     */
    public List<Bullet> bulletList = new CopyOnWriteArrayList<>();
    private boolean moveable;
    /**
     * 四个布尔类型，每一个掌管没哦一个按键
     */
    public boolean moveUp, moveRight, moveDown, moveLeft;
    public Direction direction = Direction.UP;
    /**
     * 速度变量
     */
    private int speed = 2;

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    //第三步newtankmap

//   TankMap tankMap = new TankMap(this);
    private transient Map<String,Image> imgMap = new HashMap<>();

    /**
     * 给个tank的构造函数来调用获取图片的函数
     */
    /**
     * 构造器重用
     */



    public Tank() {

        this(Constant.MARGIN_LEFT+Constant.SPRITE_SIZE*4,
                Constant.MARGIN_TOP + Constant.SPRITE_SIZE*12,
                Constant.SPRITE_SIZE,Constant.SPRITE_SIZE);
    }

    public Tank(int x, int y, int width, int height) {
        super(x, y, width, height);
        loadingImg();
    }
     //碰撞监测
    @Override
    public Rectangle getRectangle() {
        return new  Rectangle(getX(),getY(),getWidth(),getHeight());
    }
    /**
     * 移动
     */
    @Override
    public void move() {
//        if (moveable){
//            if (direction == Direction.UP){
//                /**
//                 *  x代表左右，左面是0，右面是正无穷大，y上面是0，下面是正无穷大
//                 */
//                setY(getY() - speed);
//            }
//            if (direction == Direction.RIGHT){
//                setX(getX() + speed);
//            }
//            if (direction == Direction.DOWN){
//                setY(getY() + speed);
//            }
//            if (direction == Direction.LEFT){
//                setX(getX() - speed);
//            }
//            outOfBounds();
//        }
        if (moveUp && direction == Direction.UP){
            setY(getY() - speed);
        }
        if (moveRight && direction == Direction.RIGHT){
            setX(getX() + speed);
        }
        if (moveDown && direction == Direction.DOWN){
            setY(getY() + speed);
        }
        if (moveLeft && direction == Direction.LEFT){
            setX(getX() - speed);
        }

          outOfBounds();
    }
    /**
     * 边缘检测
     */

    public void outOfBounds(){
        if (direction == Direction.UP){
            /**
             *  x代表左右，左面是0，右面是正无穷大，y上面是0，下面是正无穷大
             */
            if (getY() <= Constant.MARGIN_TOP){
                setY(Constant.MARGIN_TOP);
            }
        }
        if (direction == Direction.RIGHT){
            if (getX() >= Constant.BLACK_SIZE){
                setX(Constant.BLACK_SIZE);
            }

        }
        if (direction == Direction.DOWN){
            if (getY() >=Constant.BLACK_SIZE){
                setY(Constant.BLACK_SIZE);
            }
        }
        if (direction == Direction.LEFT){
            if (getX()<= Constant.MARGIN_LEFT){
                setX(Constant.MARGIN_LEFT);

            }
        }

    }


    /**
     * 申明一个变量，用来进行图片的切换，类似于下标那样子
     * @param g
     */
    private int index = 1;
    @Override
    public void draw(Graphics g) {
        /**
         * 图片切换
         * 如果觉得快，可以写减速器就是下面这样的
         * index+=0.2；
         * Math.floor(index);向下取整
         * 把Math.floor(index)放到index++那个地方就可以起到减速器的效果
         */
        if (direction == Direction.UP){
            g.drawImage(imgMap.get("top" + index++),getX(),getY(),null);
        }
        if (direction == Direction.RIGHT){
            g.drawImage(imgMap.get("right" + index++),getX(),getY(),null);
        }
        if (direction == Direction.DOWN){
            g.drawImage(imgMap.get("down" + index++),getX(),getY(),null);
        }
        if (direction == Direction.LEFT){
            g.drawImage(imgMap.get("left" + index++),getX(),getY(),null);
        }
        if (index > 2 ){
            index = 1;

        }
        move();
        fire();
//        g.drawImage(imgMap.get("top1"),100,100,null);
    }

    /**
     * 发射子弹开关
     */
    private boolean fire;
    private int bulletsize =1;
//发子弹get  set方法
    public int getBulletsize() {
        return bulletsize;
    }

    public void setBulletsize(int bulletsize) {
        this.bulletsize = bulletsize;
    }
//子弹get  set方法

    public void fire(){


        if (fire && /*每次至发射一次子弹bulletList.size()*/ bulletList.size() <bulletsize){
            /**
             * 发射子弹行为
             */

//            bulletList.add(new Bullet(100,100,34,34));
            if (direction == Direction.UP){
                bulletList.add(new Bullet(
                        (Constant.SPRITE_SIZE/*用坦克自己的宽度除以2*//2 + getX())-Constant.BIG_BULLET_SIZE/2,
                        getY()-Constant.BIG_BULLET_SIZE,Constant.BIG_BULLET_SIZE,
                        34,5,direction/*给子弹方向*/,this));
            }
            if (direction == Direction.RIGHT){
                bulletList.add(new Bullet(
                        getX()+Constant.BIG_BULLET_SIZE,
                        getY()+Constant.SPRITE_SIZE /2-Constant.BIG_BULLET_SIZE/2,
                        Constant.SPRITE_SIZE,Constant.BIG_BULLET_SIZE,5,direction,this/*在里面就是this*/));
            }
            if (direction == Direction.DOWN){
                bulletList.add(new Bullet(
                        (Constant.SPRITE_SIZE/*用坦克自己的宽度除以2*//2 + getX())-Constant.BIG_BULLET_SIZE/2,
                        getY()+34,Constant.BIG_BULLET_SIZE,Constant.BIG_BULLET_SIZE,5,direction,this));

            }
            if (direction == Direction.LEFT){
                bulletList.add(new Bullet(
                        getX()-Constant.BIG_BULLET_SIZE,
                        getY()+Constant.SPRITE_SIZE /2-Constant.BIG_BULLET_SIZE/2,
                        Constant.BIG_BULLET_SIZE,Constant.BIG_BULLET_SIZE,5,direction,this));

            }

        }

    }




    /**
     * 按下
     * @param e
     */
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W){
            moveUp = true;
            direction = Direction.UP;

        }
        else if (e.getKeyCode() == KeyEvent.VK_D){
            moveRight =true;
            direction = Direction.RIGHT;
        }
        else if (e.getKeyCode() == KeyEvent.VK_S){
            moveDown = true;
            direction = Direction.DOWN;
        }
        else if (e.getKeyCode() == KeyEvent.VK_A){
            moveLeft =true;
            direction = Direction.LEFT;

        }
        else if (e.getKeyCode() == KeyEvent.VK_J){
            fire = true;

        }


    }

    /**
     * 抬起
     */
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W){
            moveUp = false;
//            direction = Direction.UP;
        }
        else if (e.getKeyCode() == KeyEvent.VK_D){
            moveRight =false;
//            direction = Direction.RIGHT;
        }
        else if (e.getKeyCode() == KeyEvent.VK_S){
            moveDown = false;
//            direction = Direction.DOWN;
        }
        else if (e.getKeyCode() == KeyEvent.VK_A){
            moveLeft =false;
//            direction = Direction.LEFT;
        }else if (e.getKeyCode() == KeyEvent.VK_J){
            fire = false;

        }

    }

    /**
     * p2的按键
     */

    /**
     * 按下
     * @param e
     */
    public void keyPressed2(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_UP){
            moveUp = true;
            direction = Direction.UP;

        }
        else if (e.getKeyCode() == KeyEvent.VK_RIGHT){
            moveRight =true;
            direction = Direction.RIGHT;
        }
        else if (e.getKeyCode() == KeyEvent.VK_DOWN){
            moveDown = true;
            direction = Direction.DOWN;
        }
        else if (e.getKeyCode() == KeyEvent.VK_LEFT){
            moveLeft =true;
            direction = Direction.LEFT;

        }
        else if (e.getKeyCode() == KeyEvent.VK_NUMPAD1){
            fire = true;

        }


    }

    /**
     * 抬起
     */
    public void keyReleased2(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_UP){
            moveUp = false;
//            direction = Direction.UP;
        }
        else if (e.getKeyCode() == KeyEvent.VK_RIGHT){
            moveRight =false;
//            direction = Direction.RIGHT;
        }
        else if (e.getKeyCode() == KeyEvent.VK_DOWN){
            moveDown = false;
//            direction = Direction.DOWN;
        }
        else if (e.getKeyCode() == KeyEvent.VK_LEFT){
            moveLeft =false;
//            direction = Direction.LEFT;
        }else if (e.getKeyCode() == KeyEvent.VK_NUMPAD1){
            fire = false;

        }

    }




    /**
     * p2的按键
     */



    public void loadingImg(){
        if (level ==1){
            imgMap.put("top1",ImageUtils.getValue("tank_y_1_1"));
            imgMap.put("top2",ImageUtils.getValue("tank_y_1_2"));
            imgMap.put("right1",ImageUtils.getValue("tank_y_1_3"));
            imgMap.put("right2",ImageUtils.getValue("tank_y_1_4"));
            imgMap.put("down1",ImageUtils.getValue("tank_y_1_5"));
            imgMap.put("down2",ImageUtils.getValue("tank_y_1_6"));
            imgMap.put("left1",ImageUtils.getValue("tank_y_1_7"));
            imgMap.put("left2",ImageUtils.getValue("tank_y_1_8"));
        }
    }

}
